export default class Http {
  static httpOrigin: string = 'http://192.168.31.202:9528';

  constructor(props) {
    //请求头
    this._header = null;
    //请求方式
    this._method = null;
    //请求参数
    this._params = null;
    //请求url
    this._url = null;
  }

  static ContentTypeEnum = {
    contentTypeName: 'Content-Type',
    json: 'application/json;charset=UTF-8',
    form: 'application/x-www-form-urlencoded; charset=UTF-8',
  };
  static HttpMethod = {
    get: 'GET',
    post: 'POST',
    put: 'PUT',
    patch: 'PATCH',
    delete: 'DELETE',
  };

  /**
   * 设置请求方式
   * @param httpMethod 请求方式集合
   * @returns {function(): Http} this
   */
  static setMethod = httpMethod => {
    this._method = httpMethod;
  };

  /**
   * 发送get请求
   * @param url 请求地址
   * @param params 参数
   * @returns {function(): Http}
   */
  static get = (url, params) => {
    this.setUrlAndParams(url, params);
    this.setMethod(Http.HttpMethod.get);
    return this;
  };

  /**
   * 发送post请求
   * @param url 请求地址
   * @param params 参数
   * @returns {function(): Http}
   */
  static post = (url, params) => {
    this.setMethod(Http.HttpMethod.post);
    this.setUrlAndParams(url, params);
    return this;
  };

  /**
   * 设置Content-Type
   * @param contentType
   */
  static setContentType = contentType => {
    if (!this._header) {
      this._header = new Map();
    }
    this._header.set(Http.ContentTypeEnum.contentTypeName, contentType);
    return this;
  };

  /**
   * 设置url和参数
   * @param url String
   * @param params json
   */
  static setUrlAndParams = (url, params) => {
    this._url = url;
    this._params = params;
  };

  static execute = () => {
    this.assembleConfig();
    this.sendReq();
    // //todo 测试用。正式时不需要返回this . 而是结果值
    // return this;
  };

  /**
   * 封装参数
   */
  static assembleConfig = () => {
    //GET请求
    if (this._method === Http.HttpMethod.get) {
      //参数
      const params = Object.keys(this._params)
        .map(key => {
          return `${key}=${this._params[key]}`;
        })
        .join('&');
      this._params = `?${params}`;

      if (!this._header) {
        this._header = new Map();
      }
      //默认content-type
      if (!this._header.get(Http.ContentTypeEnum.contentTypeName)) {
        this._header.set(
          Http.ContentTypeEnum.contentTypeName,
          Http.ContentTypeEnum.form,
        );
      }
    }
  };

  static sendReq = () => {
    //GET请求
    if (this._method === Http.HttpMethod.get) {
      fetch(`${this.httpOrigin}${this._url}${this._params}`, {
        ...this._header,
      })
        .then(response => {
          console.log(response);
        })
        .catch(error => {
          console.log(error);
        });
    }

    // return this.handleRes(promise);
  };
  //
  // static handleRes = async (res: Response) => {
  //   const parsedRes = await this.parseRes(res);
  //   // 如果res.ok，则请求成功
  //   if (res.ok) {
  //     return parsedRes;
  //   }
  //   // 请求失败，返回解析之后的失败的数据
  //   const error = parsedRes;
  //   throw error;
  // };
  //
  // static parseRes = async (res: Response) => {
  //   const contentType = res.headers.get('Content-Type');
  //   // 判定返回的内容类型，做不同的处理
  //   if (contentType) {
  //     if (contentType.indexOf('json') > -1) {
  //       return await res.json();
  //     }
  //     if (contentType.indexOf('text') > -1) {
  //       return await res.text();
  //     }
  //     if (contentType.indexOf('form') > -1) {
  //       return await res.formData();
  //     }
  //     if (contentType.indexOf('video') > -1) {
  //       return await res.blob();
  //     }
  //   }
  //   return await res.text();
  // };
}
